<?php

class Utility{  
    public static function getTempUploadPath($isSessionID = false)
    {
		$result = Utility::getTempPath() . '/upload';
	
		if($isSessionID)
		{
			$result .= '/' . Utility::getSessionID();
		}
		
		if (!is_dir($result)) {
			mkdir($result);
		}
		
        return $result;
    }       
	
	public static function getTempPath()
    {
		$result = realpath(Yii::app() -> basePath . '/../temp');			
		
        return $result;
    }     
	
	public static function getBasePath(){
		return realpath(Yii::app() -> basePath . '/..');
	}
	
	public static function getSessionID()
	{
		return Yii::app() -> getSession() -> getSessionID();
	}
	
	public static function getExtension($fileName, $isDot = false)
	{
		if($fileName == "") return "";
	
		$ext = preg_split("/\./", $fileName);		
		
		$result = $ext[sizeof($ext)-1];
		
		if($isDot) $result = "." . $result;
		
		return $result;
	}
		
	public static function saveImage($fileUrl = "", $fileName = "", $subFolder = "", $isCreateThumb = false)
	{
		if($fileUrl == ""|| $fileName == "") return false;
		
		try{	
			$source = Utility::getBasePath() . "/" . $fileUrl;
			
			$destination = Utility::getBasePath() . "/images";				
			$destinationThumb = Utility::getBasePath() . "/images/thumb";
                        
			if($subFolder != "")
			{
				$destination .= "/" . $subFolder;
                                $destinationThumb .= "/" . $subFolder;
			}				
                        
                        if (!is_dir($destination)) mkdir($destination);
                        
                        if($isCreateThumb && !is_dir($destinationThumb)) mkdir($destinationThumb);                                                        
                        
			$destination .=  "/" . $fileName; // . Utility::getExtension($fileUrl, true);
                        $destinationThumb .=  "/" . $fileName;
                        
			if (copy($source, $destination)) {
                            if($isCreateThumb)
                            {
                                Utility::scaleImage($source, $destinationThumb, 150, 0);
                            }
                            
                            unlink($source);
			}
			
			return $fileName; // . Utility::getExtension($fileUrl, true);				
		}
		catch(Exception $ex)
		{
			return null;
		}		
	}
	
	public static function getBaseUrl($isStatic = false )
	{
		if($isStatic)
		{				
			return Yii::app()->baseUrl;
		}
		else
		{
			return Yii::app()->baseUrl . "/index.php";
		}
	}
	
	public static function getImageCategoryPath()
	{
		return Utility::getBasePath() . "/images/category";
	}
	
	public static function getImageCategoryUrl()
	{
		return Utility::getBaseUrl(true) . "/images/category";
	}
	
	public static function getImageProductPath()
	{
		return Utility::getBasePath() . "/images/product";
	}
	
	public static function getImageProductUrl()
	{
		return Utility::getBaseUrl(true) . "/images/product";
	}
	
	public static function getImagePath($type="")
	{
		$result = Utility::getBasePath() . "/images";
		
		if($type != "")
			$result .= "/" . $type;
		
		return $result;
	}
	
	public static function getImageUrl($type="")
	{				
		$result = Utility::getBaseUrl(true) . "/images";
		
		if($type != "")
			$result .= "/" . $type;
		
		return $result;
	}
	
	public static function getTimestamp()
	{
		return microtime(true)*10000;
	}
        
        public static function getImageName($path)
	{				
            if($path == "") return "";
            
            $temp = preg_split("/\//", $path);
            
            return $temp[count($temp) - 1];            
	}
        
        public static function scaleImage($source, $destination, $width = 0, $height = 0)
        {
            $ext = Utility::getExtension($source);
            
            $img = null;
            switch($ext)
            {
                case 'gif':
                    $img = imagecreatefromgif($source);
                    break;
                case 'jpg':
                case 'jpeg':
                    $img = imagecreatefromjpeg($source);
                    break;                
            }
            
            $_w = imagesx( $img );
            $_h = imagesy( $img );
            
            if( $_w > $width || $_h > $height ) {
                if( $_w > $width && $width != 0 ) {                    
                    $height = $width * $_h / $_w;
                } 
                
                if( $_h > $height && $height != 0 ) {                    
                    $width = $height * $_w / $_h;
                }
            }
            
            $thumb = imagecreatetruecolor($width, $height);
            // Resize
            imagecopyresized($thumb, $img, 0, 0, 0, 0, $width, $height, $_w, $_h);

            // Output
            imagejpeg($thumb, $destination, 100);
                        
            return true;
        }
}
?>
